<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doAddStudent.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. <br>
    <%-- <%
    	String sname=request.getParameter("sname");
    	String sex=request.getParameter("sex");
    	String headpic=request.getParameter("headpic");
    	
     %>
     <%=sname %>,<%=headpic%> --%>
     <%-- <%
     	ServletInputStream in=request.getInputStream();
     	int ch=-1;
     	while((ch=in.read())!=-1){
     		out.print((char)ch);
     	}
      %> --%>
      <%
      	DiskFileItemFactory df=new DiskFileItemFactory();
      	ServletFileUpload up=new ServletFileUpload(df);
      	up.setHeaderEncoding("utf-8");
      	List<FileItem> list=up.parseRequest(request);
      	
      	for(FileItem f:list){
      	   if(f.isFormField()){
      	   	  //不是文件，普通的表单元素
      	   	  out.print("<p>"+f.getFieldName()+"="+f.getString("utf-8")+"</p>");
      	   }else{//是文件
      	   	  if(f.getSize()==0)
      	   	  	continue;
      	   	  	
      	   	  //文件名，以及表单上的file元素名
      	   	  out.print("file元素名："+f.getFieldName()+",文件名："+f.getName());
      	   	  String oldName=f.getName();//
      	   	  String suffix=oldName.substring(oldName.lastIndexOf('.'));
      	   	  String newName=UUID.randomUUID()+suffix;
      	   	  f.write(new java.io.File(application.getRealPath("img"),newName));
      	   	  //f.write(new java.io.File(application.getRealPath("img"),f.getName()));	
      	  	  out.print("<img src='img/"+newName+"' width='200px' height='200px'/>");	
      	   }	
       %>
         
       <%} %>
       <h1>文件上传成功！</h1>
  </body>
</html>
